package cn.xiaopengstack.infrastructure.gateway;

import cn.xiaopengstack.domain.weixin.auth.model.valobj.IpInfoVO;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author jiangyangang
 */
@Component
public class IpHelper {

    @Resource
    private RestTemplate restTemplate;

    private static final String url = "http://ip-api.com/json?lang=zh-CN";

    /**
     * 获取IP信息
     * @return
     */
    public IpInfoVO getIpInfo(){

        IpInfoVO infoVO = restTemplate.getForObject(url, IpInfoVO.class);
        if (Objects.isNull(infoVO)) {
            return null;
        }

        if ("success".equals(infoVO.getStatus())) {
            return infoVO;
        }

        return null;
    }
}
